﻿using System;
using System.Collections.Generic;
using Dict.DataModel;
using Dict.Blo;
using Dict.Config;
using UnityEngine;

namespace UI
{
    public class UITalent
    {
        public TableTalentAdvenced data;
        public string id;


        /// <summary>
        /// 天赋是否被解锁
        /// </summary>
        public bool isUnlocked = false;

        /// <summary>
        /// 天赋是否被激活
        /// </summary>
        public bool isActived = false;

        /// <summary>
        /// 所属英雄
        /// </summary>
        public UIHero owner;

        public string Cost
        {
            get
            {
                return data.cost;
            }
        }

        public bool IsTrunk
        {
            get
            {
                return data.trunck == 1;
            }
        }

        public UITalent(TableTalentAdvenced tt, UIHero owner)
        {
            id = tt.id;
            this.data = tt;
            this.owner = owner;
        }

        public TableTalent TalentSkill
        {
            get
            {
                if (talentSkill == null && !string.IsNullOrEmpty(data.talentId))
                {
                    talentSkill = TableTalentBlo.GetTalentById(data.talentId);
                }
                return talentSkill;
            }
        }

        public List<FriendBuffer> CaculateSkillProperty(BaseAttributeData attData, BaseAttributeData perData)
        {
            List<FriendBuffer> buffOther = new List<FriendBuffer>();
            if (TalentSkill != null)
            {
                if (!string.IsNullOrEmpty(TalentSkill.Buff0) && TalentSkill.Buff0TriggerTimer == (int)Battle.Engine.TalentBuffTriggerTimer.Forever)
                {
                    TableBuff tb = DictBuffBlo.GetTableBuff(TalentSkill.Buff0);
                    if (TalentSkill.Buff0Select == (int)Battle.Engine.SelectStrategy.SELF)
                    {
                        if (tb != null)
                        {
                            GetBufferAddProp(attData, perData, tb);
                        }
                        else
                        {
                            Debug.LogError("Buffer id " + TalentSkill.Buff0 + " not exist in talent " + id);
                        }
                    }
                    else if (TalentSkill.Buff0Select - 100 == (int)Battle.Engine.SelectStrategy.ALL)
                    {
                        buffOther.Add(new FriendBuffer(tb, TalentSkill.Buff0ConditionArgs));
                    }
                }
                if (!string.IsNullOrEmpty(TalentSkill.Buff1) && TalentSkill.Buff1TriggerTimer == (int)Battle.Engine.TalentBuffTriggerTimer.Forever)
                {
                    TableBuff tb = DictBuffBlo.GetTableBuff(TalentSkill.Buff1);
                    if (TalentSkill.Buff1Select == (int)Battle.Engine.SelectStrategy.SELF)
                    {
                        if (tb != null)
                        {
                            GetBufferAddProp(attData, perData, tb);
                        }
                        else
                        {
                            Debug.LogError("Buffer id " + TalentSkill.Buff1 + " not exist in talent " + id);
                        }
                    }
                    else if (TalentSkill.Buff1Select - 100 == (int)Battle.Engine.SelectStrategy.ALL)
                    {
                        buffOther.Add(new FriendBuffer(tb, TalentSkill.Buff1ConditionArgs));
                    }
                }
                if (!string.IsNullOrEmpty(TalentSkill.Buff2) && TalentSkill.Buff2TriggerTimer == (int)Battle.Engine.TalentBuffTriggerTimer.Forever)
                {
                    TableBuff tb = DictBuffBlo.GetTableBuff(TalentSkill.Buff2);
                    if (TalentSkill.Buff2Select == (int)Battle.Engine.SelectStrategy.SELF)
                    {
                        if (tb != null)
                        {
                            GetBufferAddProp(attData, perData, tb);
                        }
                        else
                        {
                            Debug.LogError("Buffer id " + TalentSkill.Buff2 + " not exist in talent " + id);
                        }
                    }
                    else if (TalentSkill.Buff2Select - 100 == (int)Battle.Engine.SelectStrategy.ALL)
                    {
                        buffOther.Add(new FriendBuffer(tb, TalentSkill.Buff2ConditionArgs));
                    }
                }
            }
            return buffOther;
        }


        public static void GetBufferAddProp(BaseAttributeData attData, BaseAttributeData perData, TableBuff buff)
        {
            switch (buff.Factor)
            {
                case TableFactorConst.hp:
                    attData.AddProp(AttributeQueue.Hp, buff.Args1);
                    break;
                case TableFactorConst.perhp:
                    perData.AddProp(AttributeQueue.Hp, buff.Args1 * CommonMethod.tableAttributeConvert);
                    break;
                case TableFactorConst.att:
                    attData.AddProp(AttributeQueue.Attack, buff.Args1);
                    break;
                case TableFactorConst.peratt:
                    perData.AddProp(AttributeQueue.Attack, buff.Args1 * CommonMethod.tableAttributeConvert);
                    break;
                case TableFactorConst.patt:
                    perData.AddProp(AttributeQueue.Attack, buff.Args1 * CommonMethod.tableAttributeConvert);
                    break;
                case TableFactorConst.pdef:
                    attData.AddProp(AttributeQueue.PhyDefend, buff.Args1);
                    break;
                case TableFactorConst.perpdef:
                    perData.AddProp(AttributeQueue.PhyDefend, buff.Args1 * CommonMethod.tableAttributeConvert);
                    break;
                case TableFactorConst.mdef:
                    attData.AddProp(AttributeQueue.MagicDefend, buff.Args1);
                    break;
                case TableFactorConst.permdef:
                    perData.AddProp(AttributeQueue.MagicDefend, buff.Args1 * CommonMethod.tableAttributeConvert);
                    break;
                case TableFactorConst.def:
                    attData.AddProp(AttributeQueue.PhyDefend, buff.Args1);
                    attData.AddProp(AttributeQueue.MagicDefend, buff.Args1);
                    break;
                case TableFactorConst.perdef:
                    perData.AddProp(AttributeQueue.PhyDefend, buff.Args1 * CommonMethod.tableAttributeConvert);
                    perData.AddProp(AttributeQueue.MagicDefend, buff.Args1 * CommonMethod.tableAttributeConvert);
                    break;
                case TableFactorConst.hit:
                    attData.AddProp(AttributeQueue.hit, buff.Args1);
                    break;
                case TableFactorConst.dodge:
                    attData.AddProp(AttributeQueue.dodge, buff.Args1);
                    break;
                case TableFactorConst.cri:
                    attData.AddProp(AttributeQueue.cri, buff.Args1);
                    break;
                case TableFactorConst.criFactor:
                    attData.AddProp(AttributeQueue.criFactor, buff.Args1);
                    break;
                case TableFactorConst.antiCri:
                    attData.AddProp(AttributeQueue.antiCri, buff.Args1);
                    break;
                case TableFactorConst.damageAdd:
                    attData.AddProp(AttributeQueue.damageAdd, buff.Args1);
                    break;
                case TableFactorConst.damageReduce:
                    attData.AddProp(AttributeQueue.damageReduce, buff.Args1);
                    break;
                default:
                    Debug.LogError("Buffer " + buff.Id + " factor " + buff.Factor + " not support");
                    break;

                    //    /// <summary>
                    //    /// 治疗加成（百分比）
                    //    /// </summary>
                    //    public const string treatAdd = "treatAdd";


            }
        }




        private TableTalent talentSkill = null;
    }
}
